/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.schedule.executor.manager;

import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;

import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;

import com.hyts.guava.object.Objects;
import com.hyts.schedule.executor.base.AbstractTriggerManager;
import com.hyts.schedule.executor.error.ScheduleError;
import com.hyts.schedule.executor.model.SimpleTriggerBean;
import com.hyts.schedule.executor.model.TriggerBean;

/** 
 * @title DefaultTriggerManager.java  
 * @package com.hyts.schedule.quartz.manager  
 * @description 默认实现的触发器管理器-简单触发器  
 * @see com.hyts.schedule.executor.base.AbstractTriggerManager<TriggerBean>
 * 		com.hyts.schedule.quartz.model.TriggerBean
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月18日 
 * @version V1.0 
 */
public class DefaultTriggerManager implements AbstractTriggerManager<TriggerBean> {

	/* (非 Javadoc)
	 * @param trigger
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractTriggerManager#addTrigger(java.lang.Object)  
	 * @exception
	 */ 
	@Override
	public Trigger addTrigger(TriggerBean trigger) {
		//触发器操作
		Trigger triggerBean = null;
		if(trigger.getClass().isAssignableFrom(SimpleTriggerBean.class)){
			SimpleTriggerBean sTrigger = (SimpleTriggerBean)trigger;
			triggerBean = newTrigger()
					.withIdentity(sTrigger.getTriggerName(),
							sTrigger.getTriggerGroupName()).
							withSchedule(transfer(simpleSchedule(),sTrigger)).
							forJob(sTrigger.getJobBean().getJobName(), sTrigger.getJobBean().getJobGroupName()).
							startAt((sTrigger.getStartDate().getTime()
									<=System.currentTimeMillis())?new Date():
										sTrigger.getStartDate()).build();
		}else if(trigger.getClass().isAssignableFrom(CronTrigger.class)){
			CronTrigger cTrigger = (CronTrigger)trigger;
		}else {
			throw new ScheduleError("传入的触发器类型必须为TriggerBean子类型!");
		}
		return triggerBean;
	}

	/**  
	 * <b>transfer</b>
	 * <p>转换对应的时间对象为真实Quartz的触发器对象实体</p>
	 * @param build
	 * @param sTrigger
	 * @return
	 * @exception
	 */ 
	private SimpleScheduleBuilder transfer(SimpleScheduleBuilder build,SimpleTriggerBean sTrigger){
		//是否永久循环:
		if(sTrigger.isCycleForever()){
			build = build.repeatForever();
		}else {
			Objects.checkNull(sTrigger.getRepeatCount());
			build = build.withRepeatCount(sTrigger.getRepeatCount());
		}
		//是否处于单位:
		switch(sTrigger.getUnit()){
			case SECONDS:
				build = build.withIntervalInSeconds(sTrigger.getIntervalPhase());
				break;
			case MINUTES:
				build = build.withIntervalInMinutes(sTrigger.getIntervalPhase());
				break;
			case HOURS:
				build = build.withIntervalInHours(sTrigger.getIntervalPhase());
				break;
			case DAYS:
				build = build.withIntervalInHours(sTrigger.getIntervalPhase()*24);
				break;
			default:
			    throw new ScheduleError("暂时不支持该时间单位："+sTrigger.getUnit().name()+"-无法确认该时间单位");
		}
		return build;
	}
	
}
